package com.getpebble.android.framework.endpoint;

import android.content.Context;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.framework.comm.MessageRouter;
import com.getpebble.android.framework.pebblekit.PebbleKit;
import com.getpebble.android.framework.protocol.EndpointId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NormalEndpointSet extends EndpointSet {
    protected String TAG;
    private Map<EndpointId, List<RequestableEndpoint>> mEndpoints;

    public NormalEndpointSet(Context context, MessageRouter router) {
        super(router);
        this.TAG = NormalEndpointSet.class.getSimpleName();
        this.TAG = getClass().getSimpleName();
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        } else if (router == null) {
            throw new IllegalArgumentException("'router' cannot be null!");
        }
    }

    protected void onInit() {
        Context context = getContext();
        if (context == null) {
            Trace.warning(this.TAG, "onInit(): context is null");
            return;
        }
        this.mEndpoints = new HashMap();
        addToMap(new FetchAppsEndpoint(context, this));
        addToMap(new NotificationEndpoint(this, context));
        addToMap(new MusicControlEndpoint(new PblPreferences(context), context, this));
        addToMap(new TimeControlEndpoint(this));
        addToMap(new PhoneControlEndpoint(context, this));
        PutBytesEndpoint putBytesEndpoint = new PutBytesEndpoint(this);
        addToMap(putBytesEndpoint);
        addToMap(new InstallAppEndpoint(context, this, putBytesEndpoint));
        addToMap(new InstallFileEndpoint(context, this, putBytesEndpoint));
        addToMap(new CoreDumpEndpoint(context, this));
        addToMap(new DataloggingEndpoint(context, this));
        addToMap(new AppMessageEndpoint(context, this, PebbleKit.getInstance(context)));
        addToMap(new LogDumpEndpoint(context, this));
        addToMap(new CustomizeAppEndpoint(this));
        addToMap(new PingEndpoint());
    }

    private Context getContext() {
        return PebbleApplication.getAppContext();
    }

    private void addToMap(RequestableEndpoint endpoint) {
        Trace.debug(this.TAG, "AddToMap for " + endpoint.getClass().getSimpleName());
        for (EndpointId endpointId : endpoint.getSupportedEndpoints()) {
            List<RequestableEndpoint> endpointList;
            Trace.debug(this.TAG, "Adding " + endpoint.getClass().getSimpleName() + " for " + endpointId.toString());
            if (this.mEndpoints.containsKey(endpointId)) {
                endpointList = (List) this.mEndpoints.get(endpointId);
            } else {
                endpointList = new ArrayList();
            }
            endpointList.add(endpoint);
            this.mEndpoints.put(endpointId, endpointList);
        }
    }

    public boolean handleRequest(EndpointRequest endpointRequest, FrameworkState frameworkState) throws IllegalArgumentException {
        if (endpointRequest == null) {
            throw new IllegalArgumentException("'endpointRequest' cannot be null!");
        }
        List<RequestableEndpoint> handlingEndpoints = (List) this.mEndpoints.get(endpointRequest.getEndpointId());
        if (handlingEndpoints == null || handlingEndpoints.isEmpty()) {
            Trace.debug(this.TAG, "Didn't find endpoint for handling request");
            return false;
        }
        for (RequestableEndpoint endpoint : handlingEndpoints) {
            Trace.debug(this.TAG, "Sending request to " + endpoint.getClass().getSimpleName());
            if (sendRequestToEndpoint(endpoint, endpointRequest, frameworkState)) {
                return true;
            }
        }
        return false;
    }

    protected boolean sendRequestToEndpoint(RequestableEndpoint requestableEndpoint, EndpointRequest endpointRequest, FrameworkState frameworkState) {
        if (requestableEndpoint.isRequestSupported(endpointRequest)) {
            return requestableEndpoint.onRequest(endpointRequest, frameworkState);
        }
        return false;
    }

    public boolean handleMessage(ProtocolMessage protocolMessage) {
        EndpointId protocolEndpointId = EndpointId.fromCode(protocolMessage.getEndpointId());
        if (protocolEndpointId.equals(EndpointId.SYSTEM_MESSAGE)) {
            Trace.info(this.TAG, "Dropping system message");
            return true;
        }
        List<RequestableEndpoint> handlingEndpoints = (List) this.mEndpoints.get(protocolEndpointId);
        if (handlingEndpoints == null || handlingEndpoints.isEmpty()) {
            Trace.debug(this.TAG, "No endpoints exist that can handle this message with endpointId: " + protocolMessage.getEndpointId());
            return false;
        }
        for (RequestableEndpoint endpoint : handlingEndpoints) {
            if (endpoint.onReceive(protocolMessage)) {
                return true;
            }
        }
        return false;
    }

    public void onMessageSendSuccess() {
    }

    public void onMessageSendFailed() {
        Trace.debug(this.TAG, "Message send failed.");
    }

    protected void onDestroy() {
    }
}
